package com.qyy.springblog.controller;


import com.qyy.springblog.constants.Constant;
import com.qyy.springblog.model.BlogInfo;
import com.qyy.springblog.service.BlogService;
import com.qyy.springblog.utils.JWTUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
@Slf4j
@RestController
@RequestMapping("blog")
public class BlogController {

    @Autowired
    private BlogService blogService;

    @RequestMapping("/getList")
    public List<BlogInfo> queryBlogList(){
        return blogService.queryBlogList();
    }

    @RequestMapping("/countsBlog")
    public Integer countsBlog(){
        return blogService.countsBlog();
    }

    @RequestMapping("getBlogDetail")
    public BlogInfo queryBlogById(Integer blogId,HttpServletRequest request){
        log.info("publish,接收参数：blogId{}",blogId);
        BlogInfo blogInfo = blogService.queryBlogById(blogId);
        //1.获取登录用户信息
        //2.判断用户是否为作者
        String user_token = request.getHeader(Constant.USER_TOKEN_HEADER);
        Integer userId = JWTUtils.getUserIdFromToken(user_token);
        if(userId != null && userId == blogInfo.getUserId()){
            blogInfo.setIsLoginUser(true);
        }
        return blogInfo;
    }

    @RequestMapping("/add")
    public Boolean publishBlog(String title, String content, HttpServletRequest request){
        log.info("publish,接收参数：title{},content{}",title,content);
        //1.参数校验
        //2.获取当前登录用户
        if(!StringUtils.hasLength(title) ||!StringUtils.hasLength(content)){
            log.error("title or content为空");
            return false;
        }
        String user_token = request.getHeader(Constant.USER_TOKEN_HEADER);
        Integer userId = JWTUtils.getUserIdFromToken(user_token);
        if(userId == null || userId<1){
            log.error("用户未登录");
            return false;
        }
        BlogInfo blogInfo =new BlogInfo(title,content,userId);
        Integer result = blogService.publishBlog(blogInfo);
        if(result < 1){
            log.error("博客发布失败");
            return false;
        }
        return true;

    }

    /**
     * 编辑博客
     */
    @RequestMapping("/update")
    public Boolean update(Integer blogId,String title,String content){
        log.info("updateBlog,接收到的参数title{},content{}",title,content);
        if(blogId == null || !StringUtils.hasLength(title) && StringUtils.hasLength(content)){
            log.error("Id/标题/内容为空或不合法");
            return false;
        }
        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setTitle(title);
        blogInfo.setContent(content);
        Integer result = blogService.updateBlog(blogInfo);
        if(result < 1){
            log.error("更新失败！");
            return false;
        }
        return true;

    }

    /**
     * 删除博客
     */
    @RequestMapping("/delete")
    public Boolean delete(Integer blogId){
        log.info("删除博客，blogId:{}",blogId);
        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(1);

        Integer result = blogService.updateBlog(blogInfo);
        if(result < 1){
            log.error("删除失败！");
            return false;
        }
        return true;
    }
}
